package org.locker.device.controller.admin;

import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.locker.common.idempotent.annotation.RepeatSubmit;
import org.locker.common.log.annotation.Log;
import org.locker.common.web.core.BaseController;
import org.locker.common.mybatis.core.page.PageQuery;
import org.locker.common.core.domain.R;
import org.locker.common.core.validate.AddGroup;
import org.locker.common.core.validate.EditGroup;
import org.locker.common.log.enums.BusinessType;
import org.locker.common.excel.utils.ExcelUtil;
import org.locker.domain.vo.DeviceGridVo;
import org.locker.domain.bo.DeviceGridBo;
import org.locker.service.IDeviceGridService;

/**
 * 设备格子信息
 *
 * @author winnie
 * @date 2023-10-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/device/deviceGrid")
public class DeviceGridController extends BaseController {

    private final IDeviceGridService deviceGridService;

    /**
     * 查询设备格子信息列表
     */
    @SaCheckPermission("device:deviceGrid:list")
    @GetMapping("/list")
    public R<Page<DeviceGridVo>> list(DeviceGridBo bo, PageQuery pageQuery) {
        return R.ok(deviceGridService.queryPageList(bo, pageQuery));
    }

    /**
     * 导出设备格子信息列表
     */
    @SaCheckPermission("device:deviceGrid:export")
    @Log(title = "设备格子信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DeviceGridBo bo, HttpServletResponse response) {
        List<DeviceGridVo> list = deviceGridService.queryList(bo);
        ExcelUtil.exportExcel(list, "设备格子信息", DeviceGridVo.class, response);
    }

    /**
     * 获取设备格子信息详细信息
     *
     * @param deviceGridId 主键
     */
    @SaCheckPermission("device:deviceGrid:query")
    @GetMapping("/{deviceGridId}")
    public R<DeviceGridVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long deviceGridId) {
        return R.ok(deviceGridService.queryById(deviceGridId));
    }

    /**
     * 新增设备格子信息
     */
    @SaCheckPermission("device:deviceGrid:add")
    @Log(title = "设备格子信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DeviceGridBo bo) {
        return toAjax(deviceGridService.insertByBo(bo));
    }

    /**
     * 修改设备格子信息
     */
    @SaCheckPermission("device:deviceGrid:edit")
    @Log(title = "设备格子信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DeviceGridBo bo) {
        return toAjax(deviceGridService.updateByBo(bo));
    }

    /**
     * 删除设备格子信息
     *
     * @param deviceGridIds 主键串
     */
    @SaCheckPermission("device:deviceGrid:remove")
    @Log(title = "设备格子信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceGridIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] deviceGridIds) {
        return toAjax(deviceGridService.deleteWithValidByIds(List.of(deviceGridIds), true));
    }


    /**
     * 开锁
     * @param deviceGridIds
     * @return
     */
    @PostMapping("/open/{deviceGridIds}")
    public R<Void> open(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] deviceGridIds) {
        deviceGridService.open(deviceGridIds);
        return R.ok();
    }
}
